package com.talent.core.protocol.mqtt.codec.decoder;

import com.talent.core.protocol.mqtt.model.fix.TlMqttFixedHead;
import com.talent.core.protocol.mqtt.model.request.TlMqttPubAckReq;
import com.talent.core.protocol.mqtt.model.variable.TlMqttPubAckVariableHead;
import io.netty.buffer.ByteBuf;

/**
 * @ClassName：TlConnectDecoder
 * @Author: hszhou
 * @Date: 2024/11/30 15:12
 * @Description: 接收到qos1的消息确定解码器
 */
public class TlMqttPubAckDecoder {

    public TlMqttPubAckReq build(ByteBuf buf, int type, int remainingLength){
        TlMqttFixedHead fixedHead = decodeFixedHeader( type, remainingLength);
        TlMqttPubAckVariableHead variableHead = decodeVariableHeader(buf);
        return new TlMqttPubAckReq(fixedHead,variableHead);
    }

    TlMqttFixedHead decodeFixedHeader(int type,int remainingLength) {
        TlMqttFixedHead fixedHead = new TlMqttFixedHead();
        int messageType = type >> 4;
        fixedHead.setLength(remainingLength);
        fixedHead.setMessageType(messageType);
        fixedHead.setType(type);
        return fixedHead;
    }

    TlMqttPubAckVariableHead decodeVariableHeader(ByteBuf buf) {
        TlMqttPubAckVariableHead variableHead=new TlMqttPubAckVariableHead();
        int messageId = buf.readUnsignedShort();
        variableHead.setMessageId(messageId);
        return variableHead;
    }

}
